
export function getParam(url, paramName) {
  var params = url.split("?")[1].split("&");
  for (var i = 0; i < params.length; i++) {
    var param = params[i].split("=");
    if (param[0] === paramName) return decodeURIComponent(param[1]);
  }
}

/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content
  })
}

/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function(res) {
        resolve(res)
      }
    })
  })
}

export function showDeleteConfirm(content) {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title: '提示',
      content: content ? content : '确认要删除这条数据吗',
      cancelText: '取消',
      confirmText: '确定',
      confirmColor: '#dd524d',
      success: function(res) {
        resolve(res)
      }
    })
  })
}

/**
* 参数处理
* @param params 参数
*/
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    var part = encodeURIComponent(propName) + "="
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']'
            var subPart = encodeURIComponent(params) + "="
            result += subPart + encodeURIComponent(value[key]) + "&"
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&"
      }
    }
  }
  return result
}
// 手机正则
export function reg(params1){
  let phone = ''
  let code = ''

  let ret = /^1(3\d|4[5-9]|5[0-35-9]|6[567]|7[0-8]|8\d|9[0-35-9])\d{8}$/ // 手机正则
  let intPattern = /^[0-9]*$/  // 整数正则

  if(params1 === ''){
    uni.showToast({title:'请输入手1机号',icon:"none"})
  }else if( !ret.test(params1) ) {
    uni.showToast({title: '手机号不正确', icon: "none"})
  }else{
    phone = ret
    code = intPattern
  }

  return phone,code
}

export function compareVersion(version1, version2) {
  const newVersion1 = `${version1}`.split('.').length < 3 ? `${version1}`.concat('.0') : `${version1}`;
  const newVersion2 = `${version2}`.split('.').length < 3 ? `${version2}`.concat('.0') : `${version2}`;
  //计算版本号大小,转化大小
  function toNum(a){
    const c = a.toString().split('.');
    const num_place = ["", "0", "00", "000", "0000"],
        r = num_place.reverse();
    for (let i = 0; i < c.length; i++){
      const len=c[i].length;
      c[i]=r[len]+c[i];
    }
    return c.join('');
  }
  // 检测版本号是否需要更新
  function checkPlugin(a, b) {
    const numA = toNum(a);
    const numB = toNum(b);
    return numA > numB ? 1 : numA < numB ? -1 : 0;
  }
  return checkPlugin(newVersion1 ,newVersion2);
}

/**
 * 构造树型结构数据
 * @param {*} data 数据源
 * @param {*} id id字段 默认 'id'
 * @param {*} parentId 父节点字段 默认 'parentId'
 * @param {*} children 孩子节点字段 默认 'children'
 */
export function treeDataTranslate(data, id = 'id', parentId = 'parentId', children = 'children') {
  let res = []
  let temp = {}
  for (let i = 0; i < data.length; i++) {
    temp[data[i][id]] = data[i]
  }
  for (let k = 0; k < data.length; k++) {
    if (temp[data[k][parentId]] && data[k][id] !== data[k][parentId]) {
      if (!temp[data[k][parentId]][children]) {
        temp[data[k][parentId]][children] = []
      }
      temp[data[k][parentId]][children].push(data[k])
    } else {
      res.push(data[k])
    }
  }
  return res
}